/*
 * Copyright (c)  2018, XiQiao
 * All rights reserved. 
 *
 * Id:R.java   2018-06-30 16:21 wanglijun
 */
package io.marmot.web.controller;

import lombok.Data;
import org.springframework.http.HttpStatus;

/**
 * <p>
 * Title: 响应类
 * </p>
 * <p>
 * Description:响应类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2018
 * </p>
 * <p>
 * Company: XiQiao
 * </p>
 *
 * @author: wanglijun
 * @create: 2018-06-30 16:21
 * @version：1.0
 */
@Data
public class R<T> {

    /**响应代码*/
    private String code;
    /**响应消息*/
    private String message;
    /**更新时间*/
    private String updatedDate;
    /**响应内容*/
    private T body;

    /**
     * 该构造方法默认code 为200
     */
    private R() {
        this.code = HttpStatus.OK.name();
        this.message = "success";
    }

    public static <T> R ok() {
        return new R<T>();
    }

    public static <T> R ok(T body) {
        return ok().body(body);
    }

    public static <T> R ok(T body, String message) {
        return ok().body(body).message(message);
    }


    public static <T> R error() {
        return new R<T>().code(HttpStatus.INTERNAL_SERVER_ERROR.value() + "").message("未知异常，请联系管理员");
    }

    public static <T> R error(T body) {
        return error().body(body);
    }

    public static <T> R error(T body, String message) {
        return error().body(body).message(message);
    }


    public R code(String code) {
        this.code = code;
        return this;
    }

    public R body(T body) {
        this.body = body;
        return this;
    }

    public R message(String message) {
        this.message = message;
        return this;
    }

    public R time(String updateTime) {
        this.updatedDate = updateTime;
        return this;
    }
}
